import axios from 'axios'77777777777777777777777777777777777777777777777777777777777
import qs from 'qs'
import Vue from 'vue'
import { Toast } from 'vant'
import {isWeixin, weChatAuthorization, getCookie, appid, setCookie, getQueryString } from '../assets/common/js/config'
import waptoken from '../assets/common/js/wapid'
Vue.use(Toast)

// 自建axios示例
var customRequestAjax = axios.create({
  method: 'get', // 默认是 get
  baseURL: 'https://ckysmall.ckc8.com/',
  timeout: 30000
})

// token: b24vxLk284D0QSyfzxne67jIeslkhtoatK105   000ckys000D0Zo0fvxGdaxIMBtlCIYckys000  b24qrOI284D0Q65n9xnG3fuGfsmMnUoatK105

// 添加请求拦截器
customRequestAjax.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  if (isWeixin()) {
    if (getCookie('wxmalltoken')) {
      if (!getCookie('ndoresult')) {
        setCookie('ndoresult', getCookie('wxndoresult'))
      }
      if (config.method === 'post') {
        config.data = {
          ...config.data,
          'appid': 1,
          'tn': new Date().getTime(),
          'token': appid,
          'sign': new Date().getTime()
        }
        config.data = qs.stringify(config.data)
      } else if (config.method === 'get') {
        config.params = {
          'appid': 1,
          'tn': new Date().getTime(),
          'token': appid,
          'sign': new Date().getTime(),
          ...config.params
        }
      }
    } else {
      if (getQueryString('pt') == 'xcx' && getQueryString('token')) {
        if (config.method === 'post') {
          config.data = {
            ...config.data,
            'appid': 4,
            'tn': new Date().getTime(),
            'token': appid,
            'sign': new Date().getTime()
          }
          config.data = qs.stringify(config.data)
        } else if (config.method === 'get') {
          config.params = {
            'appid': 4,
            'tn': new Date().getTime(),
            'token': appid,
            'sign': new Date().getTime(),
            ...config.params
          }
        }
      } else {
        weChatAuthorization()
      }
    }
  } else {
    if (getQueryString('pt') == 'app' && getQueryString('token')) {
      if (config.method === 'post') {
        config.data = {
          ...config.data,
          'appid': 2,
          'tn': new Date().getTime(),
          'token': appid,
          'sign': new Date().getTime()
        }
        config.data = qs.stringify(config.data)
      } else if (config.method === 'get') {
        config.params = {
          'appid': 2,
          'tn': new Date().getTime(),
          'token': appid,
          'sign': new Date().getTime(),
          ...config.params
        }
      }
    } else if (getQueryString('pt') == 'ckapp' && getQueryString('token')) {
      if (config.method === 'post') {
        config.data = {
          ...config.data,
          'appid': 5,
          'tn': new Date().getTime(),
          'token': appid,
          'sign': new Date().getTime()
        }
        config.data = qs.stringify(config.data)
      } else if (config.method === 'get') {
        config.params = {
          'appid': 5,
          'tn': new Date().getTime(),
          'token': appid,
          'sign': new Date().getTime(),
          ...config.params
        }
      }
    } else {
      var wapid = waptoken.waptoken
      if (config.method === 'post') {
        config.data = {
          ...config.data,
          'appid': 3,
          'tn': new Date().getTime(),
          'token': wapid,
          'sign': new Date().getTime()
        }
        config.data = qs.stringify(config.data)
      } else if (config.method === 'get') {
        config.params = {
          'appid': 3,
          'tn': new Date().getTime(),
          'token': wapid,
          'sign': new Date().getTime(),
          ...config.params
        }
      }
    }
  }
  return config
}, (error) => {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 添加响应拦截器
customRequestAjax.interceptors.response.use(function (res) {
  // 对响应数据做点什么
  if (res.config.url.indexOf('Wxmall/Sysinfo/actNotice') <= -1) {
    if (res.config.url.indexOf('Wxmall/ShoppingCart/getShoppingCart') > -1) {
      if (res.data.code != 200 && res.data.code != 10000 && res.data.code != 2000) {
        if (res.data.msg) {
          Toast(res.data.msg)
        }
      }
    } else if (res.config.url.indexOf('Wxmall/Activity/getCkCommunityActList') > -1) {
      if (res.data.code != 200 && res.data.code == 5001 && res.data.code != 2000) {
        if (res.data.msg) {
          Toast(res.data.msg)
        }
      }
    } else if (res.config.url.indexOf('Wxmall/OrderRefund/checkOrderRefund') > -1 || res.config.url.indexOf('Wxmall/OrderRefund/addApplyOrder')) {
      if (res.data.code != 200 && res.data.code != 6006 && res.data.code != 6007 && res.data.code != 2000) {
        if (res.data.msg) {
          Toast(res.data.msg)
        }
      }
    } else if (res.config.url.indexOf('Wxmall/DrainageAct/getActJoinStatus') > -1) {
      if (res.data.code != 200 && res.data.code != 201 && res.data.code != 202 && res.data.code != 2000) {
        if (res.data.msg) {
          Toast(res.data.msg)
        }
      }
    } else {
      if (res.data.code != 200 && res.data.code != 2000) {
        if (res.config.data && res.config.data.indexOf('custom_msg_kk') >= 0) {
        } else if (res.data.msg) {
          Toast(res.data.msg)
        }
      }
    }
  }
  return res
}, function (error) {
  // 对响应错误做点什么
  // Toast.clear()
  // Toast('网络错误')
  return Promise.reject(error)
})

export {
  customRequestAjax
}
